package com.sczmgk.hotline_pzh.view;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;


/**
 * Created by Administrator on16/12/6 0006.
 */

public class MyScrollView extends ScrollView {

    private boolean isScrolledToTop = true ;

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private OnScrollChangedListener listener;

    /**
     * 定义监听接口
     */
    public interface OnScrollChangedListener {
        void onScrolledToTop();
        void onScrollChange() ;
    }

    public void setOnScrollChangedListener(OnScrollChangedListener onChangedListener) {
        listener = onChangedListener;
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (scrollY == 0) {
            isScrolledToTop = clampedY;
        } else {
            isScrolledToTop = false;
        }
        notifyScrollChangedListeners();
        if (listener != null){
            listener.onScrollChange();
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (android.os.Build.VERSION.SDK_INT < 9) {
            if (getScrollY() == 0) {
                isScrolledToTop = true;
            } else {
                isScrolledToTop = false;
            }
            notifyScrollChangedListeners();
            if (listener != null){
                listener.onScrollChange();
            }
        }
    }

    private void notifyScrollChangedListeners() {
        if (isScrolledToTop) {
            if (listener != null) {
                listener.onScrolledToTop();
            }
        }
    }

    public boolean isScrolledToTop() {
        return isScrolledToTop;
    }
}
